<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Config;

class ApiServiceData
{
    public $state;
    public $errors;
    public $isDebugg;
    public function setState($s)
    {
        $this->state = $s;
    }
    public function  getState()
    {
        return $this->state;
    }
    public function  addError($err)
    {
        $this->errors[] = $err;
    }
    public function getErrors()
    {
        return $this->errors;
    }
    public function init()
    {
        $this->state = 1;
        $this->errors = [];
        $this->isDebugg = false;
    }
    public function generateToken($argString)
    {
        return md5(strval(rand(0,100)).strval(time()).$argString);
    }
    public function setDebug($bool = true)
    {
        $this->isDebugg = $bool;
    }
    public function isDebug()
    {
        return $this->isDebugg;
    }
    public function  getCurrentTimestamp()
    {
        return date('Y-m-d H:i:s',time());
    }
    
    public function mysqlTimestampToPHP($timestamp)
    {
        return strtotime($timestamp);
    }
    
    public function isMysqlTimestampExpired($mysqlTimestamp,$spanTime)
    {
        return (time() - $this->mysqlTimestampToPHP($mysqlTimestamp)) > intval($spanTime);
    }
    
    public function isHeartbeatExpired($user)
    {
        return $this->isMysqlTimestampExpired($user->heartbeat_at,Config::get('expired.heartbeat'));
    }
    
    public function isValidateEmailExpired($user)
    {
        return $this->isMysqlTimestampExpired($user->email_token_created_at, Config::get('expired.validateemail'));
    }
    
}

class ApiServiceProvider extends ServiceProvider
{
    
    
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
        $this->app->singleton('ApiService', function ($app) {
            $s = new ApiServiceData;
            $s->init();
            return $s;
        });
    }
}
